/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "platformwindow.h"
#include <QGuiApplication>
#include <QScreen>
#include <QtPlatformHeaders/QXcbWindowFunctions>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/surface.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <kwindowsystem.h>

PlatformWindow::PlatformWindow(QWindow *parent,
                               bool panelTakefocus,
                               bool keepAbove,
                               KWayland::Client::PlasmaShellSurface::Role surfaceRole)
    : QQuickView(parent)
    , m_isWayland(false)
    , m_panelTakefocus(panelTakefocus)
    , m_keepAbove(keepAbove)
    , m_waylandSurfaceRole(surfaceRole)
{
    setColor(Qt::transparent);
    if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
        m_isWayland = true;
    }
    initWindow();
}

void PlatformWindow::initWindow()
{
    if (!m_isWayland) {
        return;
    }
    using namespace KWayland::Client;
    ConnectionThread *connection = ConnectionThread::fromApplication(this);
    if (!connection) {
        return;
    }
    Registry *registry = new Registry(this);
    registry->create(connection);

    m_surface = Surface::fromWindow(this);
    if (!m_surface) {
        return;
    }

    connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) {
        m_plasmaShellInterface = registry->createPlasmaShell(name, version, this);
        m_plasmaShellSurface = m_plasmaShellInterface->createSurface(m_surface, this);
        m_plasmaShellSurface->setSkipTaskbar(true);
        m_plasmaShellSurface->setSkipSwitcher(true);
        m_plasmaShellSurface->setRole(m_waylandSurfaceRole);
        if (m_waylandSurfaceRole == PlasmaShellSurface::Role::Panel) {
            m_plasmaShellSurface->setPanelTakesFocus(m_panelTakefocus);
            m_plasmaShellSurface->setPanelBehavior(PlasmaShellSurface::PanelBehavior::WindowsGoBelow);
        }
    });

    if (m_keepAbove) {
        connect(registry, &Registry::plasmaWindowManagementAnnounced, this, [registry](quint32 name, quint32 version) {
            PlasmaWindowManagement *m = registry->createPlasmaWindowManagement(name, version);
            connect(m, &PlasmaWindowManagement::windowCreated, registry, [] (KWayland::Client::PlasmaWindow *window) {
                qDebug() << window;
                if (window) {
                    window->requestToggleKeepAbove();
                }
            });
        });
    }

    registry->setup();
    connection->roundtrip();
}

